alert("ProfileManager.js loaded");

function ProfileManager() {
	this.profileFilename = 'profile.dat';
}

/***
 * display-time: 0 - No Início; 1 - No Meio; 2 - Ao Final; 3 - Nunca
 * interval: 0 - Agora; 1 - Restante do Dia; 2 - Específico
 */
ProfileManager.prototype.loadAppConfig = function() {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'r');
	var appConfig = {};
	var line;

	if (handler == null) {
		handler = fileSystemObject.openCommonFile(this.profileFilename, 'w');
		handler.writeLine('c|length|10');
		handler.writeLine('c|display-time|0');
		handler.writeLine('c|interval|0');
		
		appConfig['length'] = 10;
		appConfig['display-time'] = 0;
		appConfig['interval'] = 0;
	} else {
		while (((line = handler.readLine()) != null) && (line != '')) {
			if (line[0] != 'c')
				continue;
			var tokens = line.split("|");
			appConfig[tokens[1]] = tokens[2];
		}
	}
	fileSystemObject.closeCommonFile(handler);

	return appConfig;
}

ProfileManager.prototype.changeAppConfig = function(item, value) {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'r+');
	var lines = new Array();
	var line;
		
	for (var i = 0; ((line = handler.readLine()) != null) && (line != ''); i++) {
		var tokens = line.split("|");
		if (tokens[0] == 'c' && tokens[1] == item)
			lines[i] = 'c|' + item + '|' + value;
		else
			lines[i] = line;
	}
	
	handler.writeAll('');
	for (var i = 0; i < lines.length; i++)
		handler.writeLine(lines[i]);
	
	fileSystemObject.closeCommonFile(handler);
}

ProfileManager.prototype.getPreferredPrograms = function() {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'r');
	var preferredPrograms = new Array();
	var line;
	var i = 0;
	
	while (((line = handler.readLine()) != null) && (line != '')) {
		if (line[0] != 'p')
			continue;
		var tokens = line.split("|");
		preferredPrograms[i] = { program: tokens[1], channel: tokens[2], doi: tokens[3] };
		i++;
	}
	fileSystemObject.closeCommonFile(handler);

	return preferredPrograms;
}

ProfileManager.prototype.addPreferredProgram = function(program, channel) {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'w');
	
	handler.writeLine('p|' + this.previousProgram + '|' + this.previousChannel + '|' + average + "\n");
}

ProfileManager.prototype.updatePreferredProgram = function(program, channel, doi) {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'r+');
	var lines = new Array();
	var updatedLine = undefined;
	var line;

	for (var i = 0; ((line = handler.readLine()) != null) && (line != ''); i++) {
		var tokens = line.split("|");
		
		if (tokens[0] == 'p' && tokens[1] == program) {
			var programTitle = tokens[1];
			var channelName = tokens[2];
			var oldAverage = parseFloat(tokens[3]);
			var newAverage = ((doi + oldAverage) / 2).toFixed(2);

			updatedLine = 'p|' + programTitle + '|' + channelName + '|' + newAverage;
		} else {
			lines[i] = line;
		}
	}

	if (updatedLine != undefined) {
		handler.writeAll('');
		handler.writeLine(updatedLine);
		for (var i = 0; i < lines.length; i++)
			handler.writeLine(lines[i]);
	}
	fileSystemObject.closeCommonFile(handler);
}

ProfileManager.prototype.existsPreferredProgram = function(program, channel) {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'r');
	var line;
	
	while (((line = handler.readLine()) != null) && (line != '')) {
		if (line[0] != 'p')
			continue;
		var tokens = line.split("|");
		if (tokens[1] == program && tokens[2] == channel) {
			fileSystemObject.closeCommonFile(handler);
			return true;
		}
	}
	
	fileSystemObject.closeCommonFile(handler);
	return false;
}

ProfileManager.prototype.getBlackList = function () {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'r');
	var blackList = new Array();
	var line;
	var i = 0;
	
	while (((line = handler.readLine()) != null) && (line != '')) {
		if (line[0] != 'b')
			continue;
		var tokens = line.split("|");
		if (tokens[1] == 'p')
			blackList[i] = { type: 'p', value: tokens[2] };
		else if (tokens[1] == 'g')
			blackList[i] = { type: 'g', value: tokens[2] };
		i++;
	}
	fileSystemObject.closeCommonFile(handler);

	return blackList;
}

ProfileManager.prototype.addToBlackList = function(type, item) {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'r+');

	if (this.existsInBlackList(type, item))
		handler.writeLine('b|' + type + '|' + item);
	fileSystemObject.closeCommonFile(handler);
}

ProfileManager.prototype.removeFromBlackList = function(item) {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'r+');
	var lines = new Array();
	var line;

	for (var i = 0; ((line = handler.readLine()) != null) && (line != ''); i++) {
		var tokens = line.split("|");
		if (tokens[0] == 'b' && tokens[2] == item)
			continue;
		lines[i] = line;
	}
	
	handler.writeAll('');
	for (var i = 0; i < lines.length; i++)
		handler.writeLine(lines[i]);
	
	fileSystemObject.closeCommonFile(handler);
}

ProfileManager.prototype.existsInBlackList = function(type, item) {
	var fileSystemObject = new FileSystem();
	var handler = fileSystemObject.openCommonFile(this.profileFilename, 'r');
	var line;
	
	while (((line = handler.readLine()) != null) && (line != '')) {
		if (line[0] != 'b')
			continue;
		var tokens = line.split("|");
		if (tokens[1] == type && tokens[2] == item) {
			fileSystemObject.closeCommonFile(handler);
			return true;
		}
	}
	
	fileSystemObject.closeCommonFile(handler);
	return false;
}
